import axios from "axios";

function user(){
    return JSON.parse(window.sessionStorage.getItem("jsonArrayUserGroup"))[0].NumUserGroup;
}

const AccessTokens = window.localStorage.getItem("AccessToken");
const http = axios.create({
    baseURL:'http://localhost:46204/',
    headers:{authorization :AccessTokens}
})

http.interceptors.request.use((config)=>{
        switch(config.method){
        case 'post':
        case 'put':
            if(config.data ===undefined){
                config.data={CurrentUserGroup:user()};
            }
            break;
        case 'get':
        case 'delete':
           if(config.params === undefined){
                config.params={CurrentUserGroup:user()};
           }
           break;
    }
    return config
},function(error){
    return Promise.reject(error)
})
http.interceptors.response.use(function(resResult){
    if (resResult.status == 200) {
            return  resResult.data
    } else if (resResult.status == 404) {
        alert('无此接口')
    } else {
        alert('网络请求失败')
    }
},function(error){
    return Promise.reject(error)
})

export default http;